package com.launcher.wkang.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.launcher.wkang.App;
import com.launcher.wkang.R;
import com.launcher.wkang.bean.CouListBean;
import com.launcher.wkang.bean.UserInfoBean;
import com.launcher.wkang.util.NetUtils;
import com.launcher.wkang.util.Url;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class Fragment3 extends Fragment {
    private View view;
    private ImageView studentIcon;
    private TextView schoolName;
    private TextView className;
    private TextView fullName;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view =getActivity().getLayoutInflater().inflate(R.layout.fragment_layout3,null);
         initView();
        return view;
    }
    private void initData(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
        String  stuName = sharedPreferences.getString("stuName", null);
        String  stuIcon = sharedPreferences.getString("stuIcon", null);
        String  grade = sharedPreferences.getString("grade", null);
        String  school = sharedPreferences.getString("school", null);
        String  stuClass = sharedPreferences.getString("stuClass", null);
        if (!TextUtils.isEmpty(stuName)&&!TextUtils.isEmpty(stuIcon)&&!TextUtils.isEmpty(grade)&&!TextUtils.isEmpty(school)&&!TextUtils.isEmpty(stuClass)){
            setStudentInfo();
        }
            getStudentInfo();

    }
    private void initView(){
        studentIcon=view.findViewById(R.id.student_icon);
        schoolName=view.findViewById(R.id.school_name);
        className=view.findViewById(R.id.class_name);
        fullName=view.findViewById(R.id.full_name);
    }
    private Callback couListCallBack=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("Fragment2","IOException="+e.getMessage());

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("Fragment2","body="+response.body());
            String body=response.body().string();
            Log.d("Fragment2","UserInfo="+body);
            Gson gson = new Gson();
            try {
            final  UserInfoBean couListBean=gson.fromJson(body, UserInfoBean.class);
                if (couListBean != null && couListBean.getStuUserInfo() != null) {
                    SharedPreferences sharedPreferences =getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("stuName", couListBean.getStuUserInfo().getStuName());
                    editor.putString("stuIcon", couListBean.getStuUserInfo().getProfilePhoto());
                    editor.putString("grade", couListBean.getStuUserInfo().getGrade());
                    editor.putString("school", couListBean.getStuUserInfo().getSchool());
                    editor.putString("stuClass",couListBean.getStuUserInfo().getStuClass());
                    editor.commit();
                }

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       setStudentInfo();

                    }
                });

            }catch (Exception e){

            }


        }
    };

    @Override
    public void onResume() {
        super.onResume();
        initData();
        App.getINstance().setsetWaveformMode(5,1);

    }
    @Override
    public void onPause() {
        super.onPause();
//        App.getINstance().setsetWaveformMode(2,1);

    }
    private void getStudentInfo(){
        Map<String,String> map=new HashMap<String, String>();
        map.put("deviceSerial",App.getINstance().deviceId);
        Glide.get(getActivity()).clearMemory();
        NetUtils.post(Url.stuUserInfo,map,couListCallBack);
    }
    private void setStudentInfo(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
        String  stuName = sharedPreferences.getString("stuName", null);
        String  stuIcon = sharedPreferences.getString("stuIcon", null);
        String  grade = sharedPreferences.getString("grade", null);
        String  stuClass = sharedPreferences.getString("stuClass", null);
        String  school = sharedPreferences.getString("school", null);
        if (!TextUtils.isEmpty(stuName)) {
            fullName.setText(stuName);
        } else {
            fullName.setText("");
        }
        if (!TextUtils.isEmpty(stuIcon)){
            if (null!=getActivity()&&!getActivity().isFinishing()) {
                Glide.with(getActivity()).load(stuIcon).error(R.mipmap.division).skipMemoryCache(true) // 不使用内存缓存
                        .diskCacheStrategy(DiskCacheStrategy.NONE) .into(studentIcon);
            }
        }
        if (!TextUtils.isEmpty(grade)&&!TextUtils.isEmpty(stuClass)) {
            className.setText(grade+stuClass);
        } else {
            className.setText("");
        }
        if (!TextUtils.isEmpty(school)){
            schoolName.setText(school);
        }else {
            schoolName.setText("");
        }
    }
}
